package com.qwb.weibo.Service;

import com.qwb.weibo.Model.User;
import com.qwb.weibo.DAO.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Created by Jason on 2016/12/20.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired UserDao userDao;
    /**
     * 用户注册
     */
    public int register(User user,HttpSession session) {
        //向数据库中添加一条用户记录，返回的是行数
        int ret = userDao.addUsers(user);
        if( ret==0 ){
            //插入失败
            throw new RuntimeException("添加失败");
        }else{
            //将用户保存下来
            session.setAttribute("Login_User",user);
        }
           return ret;
    }

    /**
     * 用户登录
     * @param userName
     * @param passWord
     * @return
     */
    public User login(String userName, String passWord) {

        //根据用户名和密码查询用户记录并创建对象
         User user = userDao.getUserByNameAndPassword(userName,passWord);
         if( user == null){
             //用户不存在，返回异常
             throw new RuntimeException("用户未注册，请先注册再登录！");
         }else{
             //用户存在，返回user对象
             return user;
         }
    }

}
